package com.yyfax.pay.depository.controller.web;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSON;
import com.yyfax.framework.common.Log;
import com.yyfax.framework.common.Pair;
import com.yyfax.pay.common.exception.PayException;
import com.yyfax.pay.common.exception.PayExceptionCode;
import com.yyfax.pay.common.model.to.YYPayFormReq;
import com.yyfax.pay.common.model.vo.RedirectFormVO;
import com.yyfax.pay.common.model.vo.ValidResult;
import com.yyfax.pay.common.util.ValidationUtil;
import com.yyfax.pay.depository.service.inner.ChargeInnerService;
import com.yyfax.pay.depository.vo.FuiouChargeVO;
import com.yyfax.pay.order.service.PayCommonService;
import com.yyfax.pay.third.model.to.fuioudeposit.FuiouEbankFormVO;
import com.yyfax.pay.third.model.to.fuioudeposit.FuiouTransferChargeFormVO;
import com.yyfax.pay.vo.depository.EBankChargeReq;
import com.yyfax.pay.vo.depository.TransferChargeReq;

/**
 * 充值接口
 * 
 * @author buyi
 * @date 2017-08-30 10:33:11
 * @since v2.0.0
 */
@Controller
public class ChargeWebController {
	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Resource
	private ValidationUtil validationUtil;

	@Resource
	private PayCommonService payCommonService;

	@Resource
	private ChargeInnerService chargeInnerService;

	/**
	 * 富友存管网银网关请求
	 * 
	 * @author buyi
	 * @date 2017-08-29 16:13:30
	 * @since v2.0.0
	 * @param merchantCode
	 * @param sign
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(method = RequestMethod.POST, path = "/web/fuiou/charge/ebank")
	public String ebank(YYPayFormReq req, Model model) throws Exception {
		// 校验绑定参数
		Pair<ValidResult, EBankChargeReq> pair = validationUtil.validateForWeb(req, EBankChargeReq.class);
		if (!pair.first().isOk()) {
			throw PayExceptionCode.ERROR_ILL_PARAMETER.expMsg(pair.first().getErrMsg());
		}

		// 校验签名
		boolean isSign = payCommonService.validSign(req.getMerchantCode(), pair.second(), req.getSign());
		if (!isSign) {
			// 如果验签不通过，抛出异常，由异常处理统一跳转页面
			throw PayExceptionCode.ERROR_SIGN_FAIL.exp();
		}

		// 持久化订单
		FuiouChargeVO vo = chargeInnerService.saveCharge(req.getMerchantCode(), pair.second());
		if (vo.isResult()) {
			// 如果订单已经结果，抛出异常，由异常处理统一跳转页面
			logger.debug(Log.op("ChargeWebController.ebank").msg("网关请求网银已经出结果，直接跳转到结果页面").kv("busiId", vo.getPo().getBusiId()).toString());
			throw new PayException(vo.getPo().getRspCode(), vo.getPo().getRspMsg());
		}

		// 封装form表单
		RedirectFormVO<FuiouEbankFormVO> routeFormVO = chargeInnerService.getEbankForm(vo);
		model.addAttribute("url", routeFormVO.getUrl());
		model.addAttribute("form", JSON.toJSON(routeFormVO.getParams()));

		return "fuioudeposit/guide";
	}

	/**
	 * 转账充值<br>
	 * 转账充值同步返回结果，只能代表充值订单的受理结果，充值行为是用户线下实际操作的。
	 * 
	 * @author buyi
	 * @date 2018-03-28 10:49:22
	 * @since v2.1.3
	 * @param req
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/web/fuiou/charge/transfer")
	public String transfer(YYPayFormReq req, Model model) throws Exception {
		// 校验绑定参数
		Pair<ValidResult, TransferChargeReq> pair = validationUtil.validateForWeb(req, TransferChargeReq.class);
		if (!pair.first().isOk()) {
			throw PayExceptionCode.ERROR_ILL_PARAMETER.expMsg(pair.first().getErrMsg());
		}

		boolean isSign = payCommonService.validSign(req.getMerchantCode(), pair.second(), req.getSign());
		if (!isSign) {
			// 如果验签不通过，抛出异常，由异常处理统一跳转页面
			throw PayExceptionCode.ERROR_SIGN_FAIL.exp();
		}

		// 持久化订单
		FuiouChargeVO vo = chargeInnerService.saveCharge(req.getMerchantCode(), pair.second());
		// 判断订单结果
		if (vo.isResult()) {
			// 如果订单已经结果，抛出异常，由异常处理统一跳转页面
			logger.debug(Log.op("ChargeWebController.transfer").msg("网关请求转账充值已经出结果，直接跳转到结果页面").kv("busiId", vo.getPo().getBusiId()).toString());
			throw new PayException(vo.getPo().getRspCode(), vo.getPo().getRspMsg());
		}

		// 封装form表单
		RedirectFormVO<FuiouTransferChargeFormVO> routeFormVO = chargeInnerService.getTransfer(vo);
		model.addAttribute("url", routeFormVO.getUrl());
		model.addAttribute("form", JSON.toJSON(routeFormVO.getParams()));

		return "fuioudeposit/guide";
	}
}
